<?php
/**
 * Note:方法内部返回
 * User:端木凌风
 * Date:2021-09-06 11:08
 * @param $status
 * @param string $msg
 * @param array $data
 * @return array
 */
function funReturn($status, $msg = '', $data = [])
{
    return [
        'status' => $status,
        'msg' => $msg,
        'data' => $data
    ];
}


/**
 * Note:接口返回
 * User:端木凌风
 * Date:2021-09-06 11:09
 * @param $status
 * @param string $msg
 * @param array $data
 * @return \Illuminate\Http\JsonResponse
 */
function apiResponse($status, $msg = '', $data = [])
{
    $returnData = [
        'status' => $status,
        'msg' => $msg,
        'data' => $data,
        'time' => date('Y-m-d H:i:s')
    ];
    return json()->data($returnData);
}

/**
 * Note:接口返回xml
 * Date:2022-11-25 19:00
 * @param $status
 * @param $msg
 * @param $data
 * @return void
 */
function xmlResponse($status, $msg = '', $data = [])
{
    $returnData = [
        'status' => $status,
        'msg' => $msg,
        'data' => $data,
        'time' => date('Y-m-d H:i:s')
    ];
    return xml()->data($returnData);
}


/**
 * Note:校验手机号
 * User:端木凌风
 * Date:2022-10-19 16:47
 * @param $mobile
 * @return bool
 */
function checkMobile($mobile)
{
    if (preg_match('/^\s*(1\d{10})\s*$/', $mobile))
        return true;
    return false;
}

/**
 * Note:转换分页起始值
 * User:端木凌风
 * Date:2021-11-08 18:52
 * @param $page
 * @param $limit
 * @return array
 */
function turnPageToStartEnd($page, $limit)
{
    $data = [
        'start' => 0,
        'limit' => $limit,
    ];
    $data['start'] = ($page - 1) * $limit;
    return $data;
}

function formatPager($total, $rs, $page, $size = 0)
{
    $pageSize = ($size > 0) ? $size : 10;
    $pageSize = $pageSize > 100 ? 100 : $pageSize;
    $totalPage = ($total % $pageSize == 0) ? ($total / $pageSize) : (intval($total / $pageSize) + 1);
    return ['total' => $total, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => $totalPage, 'data' => $rs];
}

/**
 * Note:
 * User:端木凌风
 * Date:2021-10-28 17:57
 * @param $password
 * @param $salt
 * @param int $passwordType
 * @return string
 */
function encryptPassword($password, $salt, $passwordType = 0)
{
    if ($salt) {
        $password = $salt . $password . $salt;
    } else {
        $password = 'LFVEEKER' . $password . 'LFVEEKER';
    }
    switch ($passwordType) {
        case 0:
            $password = md5($password);
            break;
        case 1:
            $password = sha1($password);
            break;
        default:
            break;
    }
    return $password;
}


/**
 * 创建加密随机字符
 * @return string
 * @date 2020-07-27 11:17
 */
function createSalt()
{
    $str = 'abcdefghijklmnopqrstuvwxyz0123456789';
    $strLen = strlen($str);
    return $str[mt_rand(0, $strLen - 1)] . $str[mt_rand(0, $strLen - 1)] . $str[mt_rand(0, $strLen - 1)] . $str[mt_rand(0, $strLen - 1)];
}

/**
 * Note:过滤header数据
 * User:端木凌风
 * Date:2022-11-25 15:36
 * @return void
 */
function filterHeaderData($headers = [])
{
    $returnData = [];
    $fields = ['authorization'];
    foreach ($headers as $key => $value) {
        if (in_array($key, $fields)) $returnData[$key] = $value;
    }
    return $returnData;
}